clear all;
clc;

c = 10;

load mnist_train;
load mnist_test;
n  = size(xL,2)+size(xU,2);
X = [xL,xU;ones(1,n)];
s = size(X,1);
TT = inv(X*X');
clear xL;
clear xU;
clear yU;
Y = yL';

nump = c*(c-1)/2;
sz = zeros(c,1);
idx = cell(10,1);
for i = 1:c
    idx{i}=find(Y==(i-1));
    sz(i)=size(idx{i},1);
end

ix = cell(c,c);
Y = cell(c,c);

for i = 1:c
    for j = i+1:c
        ix{i,j} = [idx{i};idx{j}];
        Y{i,j} = [ones(sz(i),1);-ones(sz(j),1)];
    end
end

disp('Start optimization...');
tic

fai = zeros(n,nump);

ct = 1;
for i = 1:c
    for j = i+1:c
        tic
        cvx_begin sdp
        cvx_solver sedumi
        variable phi(n,1)
        variable phx(s,1)
        minimize (phi'*phi+phx'*TT*phx);
        phx == X*phi;
        Y{i,j}.*phi(ix{i,j}) >= ones(sz(i)+sz(j),1);
        cvx_end
        fai(:,ct) = phi;
        ct = ct+1;
        toc
    end
end

